package com.b2c.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.b2c.product.mapper.SpecGroupMapper;
import com.b2c.product.pojo.dto.SpecGroupDto;
import com.b2c.product.pojo.entity.SpecGroup;
import com.b2c.product.pojo.vo.SpecGroupVo;
import com.b2c.product.service.SpecGroupService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import content.enumeration.DeletedStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import result.Result;
import util.AdminUserToken;
import util.ThreadLocalUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static content.common.ResultMessage.*;

/**
 * @author Administrator
 * @description 针对表【mall_spec_group(品类表)】的数据库操作Service实现
 * @createDate 2024-03-20 17:29:59
 */
@Service
@Slf4j
public class SpecGroupServiceImpl extends ServiceImpl<SpecGroupMapper, SpecGroup>
        implements SpecGroupService {
    /**
     * 根据id获取品类
     *
     * @param id
     * @return
     */
    @Override
    public Result queryById(Integer id) {
        SpecGroup spec = getById(id);
        if (spec == null) {
            return Result.fail(NO_THIS_SPEC);
        }
        return Result.success(spec);
    }

    /**
     * 获取所有的品类
     *
     * @return
     */
    @Override
    public Result getAll() {
        List<SpecGroup> specGroups = list();
        if (specGroups.isEmpty()) {
            return Result.fail(NO_SPEC);
        }
        return Result.success(specGroups);
    }

    /**
     * 修改状态
     * @param status
     * @param specGroupId
     * @return
     */
    @Override
    public Result updateStatus(DeletedStatus status, Integer specGroupId) {
        UpdateWrapper<SpecGroup> updateWrapper = new UpdateWrapper<SpecGroup>().eq("id", specGroupId).set("status", status);
        boolean isSuccess = update(updateWrapper);
        if (!isSuccess){
            return Result.fail(UPDATE_ERROR);
        }
        return Result.success();
    }

    /**
     * 添加品类
     * @param specGroupDto
     * @return
     */
    @Override
    public Result insert(SpecGroupDto specGroupDto) {
        Integer userId = new ThreadLocalUtil<AdminUserToken>().get().getId();
        log.info("添加品类:{}",specGroupDto);
        //构建品类实体
        SpecGroup specGroup = BeanUtil.copyProperties(specGroupDto, SpecGroup.class);
        specGroup.setCreateUserId(userId);
        boolean isSave = save(specGroup);
        if (!isSave){
            return Result.fail(ISNERT_ERROR);
        }
        return Result.success();

    }

    /**
     * 获取所有的品类
     * @return
     */
    @Override
    public Result getAllSpecGroup() {
        //查找数据库
        QueryWrapper<SpecGroup> queryWrapper = new QueryWrapper<SpecGroup>().eq("status",1);
        List<SpecGroup> specGroups = list(queryWrapper);
        if (specGroups.isEmpty()) {
            return Result.fail(NO_SPEC);
        }
        //转换成Vo
        ArrayList<SpecGroupVo> specGroupVos = new ArrayList<SpecGroupVo>();
        for (SpecGroup specGroup : specGroups) {
            SpecGroupVo specGroupVo = new SpecGroupVo();
            BeanUtils.copyProperties(specGroup,specGroupVo);
            specGroupVos.add(specGroupVo);
        }
        return Result.success(specGroupVos);
    }


}




